#include <gtest/gtest.h>
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
#include "log.h"
#include "blockqueue.h"

namespace WebServerTest
{
    using namespace std;
    class LogTest : public ::testing::Test
    {
    protected:
        void SetUp() override
        {
            Log::Instance()->init(1, "./log", ".log", 1024);
        }

        void TearDown() override
        {
            // Log::Instance()->flush();
        }
    };

    TEST_F(LogTest, LogInfo)
    {
        // 因为是另一个线程在写入程序，可能出现还没有开始写，就读去文件，那个时候啥也读不到
        LOG_INFO("This is a log info message");
        LOG_INFO("This is a log info message with %d", 10);
        LOG_INFO("This is a log info message with %s", "string");
        sleep(1);
        Log::Instance()->flush();

        time_t timer = time(nullptr);
        struct tm *sysTime = localtime(&timer);
        struct tm t = *sysTime;
        char fileName[256] = {0};
        snprintf(fileName, 256 - 1, "%s/%04d_%02d_%02d%s",
                 "./log", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, ".log");
        ifstream ifs(fileName);

        string last_one, curr_line, last_two, last_three;
        getline(ifs, last_three);
        getline(ifs, last_two);
        getline(ifs, last_one);
        while (getline(ifs, curr_line))
        {
            last_three = move(last_two);
            last_two = move(last_one);
            last_one = move(curr_line);
        }
        ifs.close();
        // cout << last_one << endl;
        // cout << last_two << endl;
        // cout << last_three << endl;
        EXPECT_NE(last_one.find("This is a log info message with string"), std::string::npos);
        EXPECT_NE(last_two.find("This is a log info message with 10"), std::string::npos);
        EXPECT_NE(last_three.find("This is a log info message"), std::string::npos);
    }

    TEST_F(LogTest, LogWarn)
    {
        LOG_WARN("This is a log warn message");
        sleep(1);
        Log::Instance()->flush();
        time_t timer = time(nullptr);
        struct tm *sysTime = localtime(&timer);
        struct tm t = *sysTime;
        char fileName[256] = {0};
        snprintf(fileName, 256 - 1, "%s/%04d_%02d_%02d%s",
                 "./log", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, ".log");
        ifstream ifs(fileName);
        string curr_line, last_one;
        getline(ifs, last_one);
        while (getline(ifs, curr_line))
        {
            last_one = move(curr_line);
        }
        ifs.close();
        EXPECT_NE(last_one.find("This is a log warn message"), std::string::npos);
        EXPECT_NE(last_one.find("warn"), std::string::npos);
    }

    TEST_F(LogTest, LogError)
    {
        LOG_ERROR("This is a log error message");
        sleep(1);
        Log::Instance()->flush();
        time_t timer = time(nullptr);
        struct tm *sysTime = localtime(&timer);
        struct tm t = *sysTime;
        char fileName[256] = {0};
        snprintf(fileName, 256 - 1, "%s/%04d_%02d_%02d%s",
                 "./log", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, ".log");
        ifstream ifs(fileName);
        string curr_line, last_one;
        getline(ifs, last_one);
        while (getline(ifs, curr_line))
        {
            last_one = move(curr_line);
        }
        ifs.close();
        EXPECT_NE(last_one.find("This is a log error message"), std::string::npos);
        EXPECT_NE(last_one.find("error"), std::string::npos);

    }
};